package com.e_manager.soft.emanager.controller;

import com.e_manager.soft.emanager.annotation.Authorization;
import com.e_manager.soft.emanager.annotation.BaseController;
import com.e_manager.soft.emanager.annotation.CurrentUser;
import com.e_manager.soft.emanager.entity.MstParameter;
import com.e_manager.soft.emanager.entity.User;
import com.e_manager.soft.emanager.resultMsg.ResultConstant;
import com.e_manager.soft.emanager.resultMsg.ResultInfo;
import com.e_manager.soft.emanager.service.MstParameterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author : lip[lip4051@163.com]
 * @date : 2018/4/3 0003 上午 11:25
 */
@BaseController
@Authorization
@Api(value = "参数", description = "用户参数")
public class ParameterController {
    @Autowired
    private MstParameterService mstParameterService;

    @ApiOperation(value = "添加用户参数")
    @PostMapping(value = "/insertParameter", produces = {"application/json;charset=utf-8"})
    public ResultInfo insertParameter(@RequestBody MstParameter mstParameter, @CurrentUser User iuser) {
        ResultInfo<?> info = new ResultInfo();
        if (mstParameter != null) {
            mstParameter.setCreateUser(iuser.getUserID());
            mstParameterService.insertParameter(mstParameter);
            info.setMsg(ResultConstant.SUCCESS.getMessage());
            info.setCode(ResultConstant.SUCCESS.getCode());
        } else {
            info.setMsg(ResultConstant.PARAMS_ERROR.getMessage());
            info.setCode(ResultConstant.PARAMS_ERROR.getCode());
        }
        return info;
    }

    @ApiOperation(value = "查询用户参数")
    @PostMapping(value = "/findParameterByType", produces = {"application/json;charset=utf-8"})
    public ResultInfo<List<MstParameter>> findParameterByType(@RequestParam("type") String type) {
        ResultInfo<List<MstParameter>> info = new ResultInfo<>();
        List<MstParameter> list = null;
        if (type != null && !"".equals(type.trim())) {
            list = mstParameterService.findParameterByType(type);
            info.setData(list);
            info.setMsg(ResultConstant.SUCCESS.getMessage());
            info.setCode(ResultConstant.SUCCESS.getCode());
        } else {
            info.setMsg(ResultConstant.PARAMS_ERROR.getMessage());
            info.setCode(ResultConstant.PARAMS_ERROR.getCode());
        }
        return info;
    }

}
